package com.yanqu.road.server.cmd.estatewar.activity.cross;

import com.yanqu.road.entity.enums.activity.palace.ePalaceAideDismissType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.estatewar.palace.UserPalaceData;
import com.yanqu.road.entity.log.LogPalaceAideChange;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.cmd.base.AbstractServerCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.palace.PalaceModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.palace.PalaceActivity;
import com.yanqu.road.server.manger.palace.PalaceActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;


@Cmd(code = Protocol.S_PALACE_CROSS_AIDE_NOTIFY, desc = "跨服幕宾通知")
public class CrossPalaceAideNotifyCmd extends AbstractServerCommand {
	private int activityId;
	private long userId;
	private int underAide;
	private int type;
	private int oldUnderAide;
	private long opUserId;
	private long opServerId;
	private String opNickName;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		EstateWarProto.PalaceAideNotifyMsg reqMsg = EstateWarProto.PalaceAideNotifyMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		userId = reqMsg.getUserId();
		underAide = reqMsg.getUnderAide();
		type = reqMsg.getType();
		opUserId = reqMsg.getOpUserId();
		if(ePalaceAideDismissType.PlayerDismiss.getValue() == type || ePalaceAideDismissType.SystemDismiss.getValue() == type){
			oldUnderAide = reqMsg.getOldUnderAide();
			opServerId = reqMsg.getOpServerId();
			opNickName = reqMsg.getOpNickName();
		}
		return true;
	}

	@Override
	public void execute(YanQuMessage packet) throws Exception {
		PalaceActivity palaceActivity = PalaceActivityMgr.getPalaceActivity(activityId);
		if(null != palaceActivity){
			UserPalaceData userPalaceData = palaceActivity.getUserPalaceData(userId);
			if(null != userPalaceData) {
				userPalaceData.setUnderAide(underAide);
				GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
				if(null != player){
					player.getModule(PalaceModule.class).syncUserData();
				}
				String title;
				String content;
				if(ePalaceAideDismissType.PlayerDismiss.getValue() == type){
					String language = UserMgr.getLanguage(userId);
					title = MultipleLanguageMgr.getContent(MailManager.PALACE_AIDE_DISMISS_TITLE, language);
					content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.PALACE_AIDE_DISMISS_CONTENT, language),
							palaceActivity.getTitleTypeName(oldUnderAide, language),
							ServerListMgr.getServerName(opServerId, language), opNickName);
					MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), "", content, title);
				}else if(ePalaceAideDismissType.SystemDismiss.getValue() == type){
					String language = UserMgr.getLanguage(userId);
					title = MultipleLanguageMgr.getContent(MailManager.PALACE_AIDE_SYSTEM_DISMISS_TITLE, language);
					content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.PALACE_AIDE_SYSTEM_DISMISS_CONTENT, language),
							palaceActivity.getTitleTypeName(oldUnderAide, language),
							ServerListMgr.getServerName(opServerId, language), opNickName);
					MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), "", content, title);
				}
				AutoLogMgr.add(new LogPalaceAideChange(activityId, userId, type, opUserId, underAide));
			}
		}
	}

	@Override
	public void sendData(YanQuMessage packet) throws Exception {

	}

}
